package com.modularity.camera.basic.creator;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;

import com.modularity.camera.basic.annotation.KeepNotProguard;
import com.modularity.camera.basic.base.CameraFragment;
import com.modularity.camera.basic.camera1.Camera1BasicFragment;
import com.modularity.camera.basic.camera1.Camera1BasicTVFragment;
import com.modularity.camera.basic.camera2.Camera2BasicFragment;
import com.modularity.camera.basic.enums.CameraFace;
import com.modularity.camera.basic.enums.CameraType;

public class CameraViewCreator {

    @KeepNotProguard
    public static CameraFragment create(Context context, CameraFace cameraFace, CameraType cameraType) {
        if (cameraType == CameraType.DEFAULT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && hasCamera2(context)) {
                return Camera2BasicFragment.newInstance(cameraFace);
            } else {
                return Camera1BasicFragment.newInstance(cameraFace);
            }
        } else if (cameraType == CameraType.CAMERA1) {
            return Camera1BasicFragment.newInstance(cameraFace);
        } else if (cameraType == CameraType.CAMERA2 && hasCamera2(context)) {
            return Camera2BasicFragment.newInstance(cameraFace);
        }
        return Camera1BasicFragment.newInstance(cameraFace);
    }

    @KeepNotProguard
    public static CameraFragment create(int cameraId) {
        return Camera1BasicFragment.newInstance(cameraId);
    }

    @KeepNotProguard
    public static CameraFragment create_tv(int cameraId) {
        return Camera1BasicTVFragment.newInstance(cameraId);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private static boolean hasCamera2(Context context) {
        if (context == null) return false;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return false;
        try {
            CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            assert manager != null;
            String[] idList = manager.getCameraIdList();
            boolean notNull = true;
            if (idList.length == 0) {
                notNull = false;
            } else {
                for (final String str : idList) {
                    if (str == null || str.trim().isEmpty()) {
                        notNull = false;
                        break;
                    }
                    final CameraCharacteristics characteristics = manager.getCameraCharacteristics(str);

                    Integer iSupportLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
                    if (iSupportLevel != null && iSupportLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
                        notNull = false;
                        break;
                    }
                }
            }
            return notNull;
        } catch (Throwable ignore) {
            return false;
        }
    }

}
